package jp.co.sega.sonic_jump.monthly.sum.PlatformStandard;

import android.util.Log;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;
import jp.co.sega.sonic_jump.monthly.sum.Game_Draw;
import jp.co.sega.sonic_jump.monthly.sum.Graphics;
import jp.co.sega.sonic_jump.monthly.sum.Shell;
import jp.co.sega.sonic_jump.monthly.sum.Text;

public class Paying implements Runnable, Define {
    public static final int ACTIVE_TIMES = 2;
    public static final int CANCEL_PAY = 1;
    public static final int CONTINUE_GAME = 2;
    public static int FONT_H = 26;
    private static final String GAME_ID = "SEGA000001";
    private static final String LOOKUP_URL = "http://www.ipxcn.com/lookup";
    private static final int METHOD_GET = 0;
    private static final int METHOD_POST = 1;
    public static final int MODE_CHECK = 0;
    public static final int MODE_CONFIRM = 1;
    public static final int MODE_CONTINUE = 5;
    public static final int MODE_FAIL = 4;
    public static final int MODE_SUCCESS = 3;
    public static final int MODE_WAITING = 2;
    public static final int NEXT_MODE = 0;
    private static final String PASSWORD = "K9%25e%263b%23";
    private static final int POINT_NUM = 6;
    public static final int SAVE_GAME = 3;
    private static final int TOTAL_PRICE = 6;
    public static final int TRY_TIMES = 5;
    private static final String USER_NAME = "sega-cn";
    private static final boolean USE_CMCC_PROXY = false;
    private static int _type = 0;
    private static int activateCnt = 0;
    private static byte[] bPayStatus = new byte[6];
    private static String[] confirmStr = {"您想和音速小子索尼克一起并肩抵御弹头博士的邪恶计划吗？购买本游戏，您只需一次性支付6元来激活游戏，一次支付，终生使用！请于2013年5月31日前购买。是否购买？"};
    public static boolean down = false;
    private static final String failStr = "||激活失败";
    private static String hRet = Define.PACKAGE_ID;
    private static final int iMethod = 0;
    private static boolean m_bPayed = false;
    private static boolean m_bScrollEnd = false;
    static Paying p = null;
    private static int paidPrice = 0;
    public static boolean payAvailable = false;
    private static int payMode = 0;
    private static final String[] payTitle = {"激活游戏", "复活", "购买星座卡片", "购买游戏币"};
    private static int payType = 0;
    public static int payingState = 0;
    private static String pinID = Define.PACKAGE_ID;
    private static final int[] pointPaidPrice = new int[4];
    private static final int[] pointTotalPrice = {6, 2, 2, 2};
    private static int smsCount = 0;
    private static String smsKeyword = Define.PACKAGE_ID;
    private static String smsShortcode = Define.PACKAGE_ID;
    private static int smsSinglePrice = 0;
    public static int startLine = 0;
    private static String status = Define.PACKAGE_ID;
    public static String[] strBack = null;
    private static final String successStr = "您已购买成功|请继续游戏。||祝您游戏愉快！";
    public static int totalLines = 0;
    public static boolean up = false;
    private static final String waitingStr = "||正在激活游戏|请稍候……";
    public int mode;

    private Paying(int mode2, int type) {
        /*_type = type;
        this.mode = mode2;
        new Thread(this).start();*/
    }

    public static void init() {
        /*smsShortcode = Define.PACKAGE_ID;
        smsKeyword = Define.PACKAGE_ID;
        confirmStr[0] = "您是否已经被风趣可爱的人物们感动了呢？帮助麦兜实现马尔代夫之旅的梦想吧！购买本游戏，您只需一次性支付6元来激活游戏，一次支付，终生使用！请于2013年5月31日前购买。是否购买？";
        payAvailable = false;
        smsCount = 1;*/
    }

    private static void updateConfirmStr() {
        /*String str;
        if (paidPrice > 0) {
            String[] strArr = confirmStr;
            StringBuilder append = new StringBuilder("感谢使用北京世嘉无线娱乐科技有限公司的游戏产品，您已经付费").append(paidPrice).append("元，还需付费").append(pointTotalPrice[0] - paidPrice).append("元，将产生").append((pointTotalPrice[0] - paidPrice) / smsSinglePrice).append("条计费短信，每条短信信息费").append(smsSinglePrice).append("元，");
            if ((pointTotalPrice[0] - paidPrice) / smsSinglePrice > 1) {
                str = "当前发送第" + smsCount + "条，";
            } else {
                str = Define.PACKAGE_ID;
            }
            strArr[0] = append.append(str).append("|客服电话：010-58695258-306").toString();
            return;
        }
        confirmStr[0] = "感谢使用北京世嘉无线娱乐科技有限公司的游戏产品，该产品资费" + pointTotalPrice[0] + "元，将产生" + (pointTotalPrice[0] / smsSinglePrice) + "条计费短信，每条短信信息费" + smsSinglePrice + "元，" + (pointTotalPrice[0] / smsSinglePrice > 1 ? "当前发送第" + smsCount + "条，" : Define.PACKAGE_ID) + "|客服电话：010-58695258-306";*/
    }

    private int getSMSCode() {
        /*Exception e;
        int iSmsCount;
        int iResult = -1;
        InputStream is = null;
        try {
            HttpURLConnection c = (HttpURLConnection) new URL(String.valueOf(LOOKUP_URL) + "?" + ("IPAddress=0.0.0.1&ProductionId=SEGA000001&Message=%23NULL%23&TotalPrice=" + (pointTotalPrice[0] - pointPaidPrice[0]) + "&Username=" + USER_NAME + "&Password=" + PASSWORD)).openConnection();
            c.setRequestMethod("GET");
            if (0 != 0) {
                c.setRequestProperty("X-Online-Host", (String) null);
            }*/
            //c.setRequestProperty("accept", "application/vnd.oma.dd+xml, */*");
            /*c.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/CLDC-1.0");
            c.setRequestProperty("Content-Length", "0");
            c.setRequestProperty("Content-Language", "en-US");
            c.setConnectTimeout(20000);
            System.out.println("connecting...");
            int rc = c.getResponseCode();
            System.out.println("rc:" + rc);
            if (rc == 200) {
                is = c.getInputStream();
            }
            System.out.println("is:" + is);
            int len = is.available();
            System.out.println("len:" + len);
            if (len > 0) {
                int actual = 0;
                int bytesread = 0;
                byte[] data = new byte[len];
                while (bytesread != len && actual != -1) {
                    actual = is.read(data, bytesread, len - bytesread);
                    bytesread += actual;
                }
                String content = new String(data);
                try {
                    System.out.println("content:" + content);
                    try {
                        String temp = content.substring(content.indexOf("<shortcode>") + 11);
                        smsShortcode = temp.substring(0, temp.indexOf("</shortcode>"));
                        System.out.println("shortcode:" + smsShortcode);
                        String temp2 = content.substring(content.indexOf("<keyword>") + 9);
                        smsKeyword = temp2.substring(0, temp2.indexOf("</keyword>"));
                        System.out.println("keyword:" + smsKeyword);
                        String temp3 = content.substring(content.indexOf("<count>") + 7);
                        String count = temp3.substring(0, temp3.indexOf("</count>"));
                        System.out.println("count:" + count);
                        String temp4 = content.substring(content.indexOf("<TransactionId>") + 15);
                        pinID = temp4.substring(0, temp4.indexOf("</TransactionId>"));
                        System.out.println("pinID:" + pinID);
                        String temp5 = content.substring(content.indexOf("<responseCode>") + 14);
                        if (temp5.substring(0, temp5.indexOf("</responseCode>")).equals("0")) {
                            if (smsShortcode.equals(Define.PACKAGE_ID)) {
                                smsShortcode = "1252013917353463";
                            }
                            if (smsKeyword.equals(Define.PACKAGE_ID)) {
                                smsKeyword = "Android Maidou SMS Error";
                            }
                            if (count.equals("null")) {
                                iSmsCount = 1;
                            } else {
                                iSmsCount = Integer.parseInt(count);
                            }
                            smsSinglePrice = (pointTotalPrice[payType] - pointPaidPrice[payType]) / iSmsCount;
                            iResult = Game_Draw.SFX_last;
                            updateConfirmStr();
                            String str = content;
                        }
                    } catch (Exception e2) {
                        System.out.println(e2);
                    }
                    String str2 = content;
                } catch (Exception e3) {
                    e = e3;
                    String str3 = content;
                    e.printStackTrace();
                    return iResult;
                }
            }
            if (is != null) {
                is.close();
            }
            if (c != null) {
                c.disconnect();
            }
        } catch (Exception e4) {
            e = e4;
            e.printStackTrace();
            return iResult;
        }
        return iResult;*/
        return 0;
    }

    private int activeGame() {
        /*System.out.println("sendMSG to:" + smsShortcode + " key:" + smsKeyword + "#" + pinID + " $" + smsSinglePrice);
        int[] iArr = pointPaidPrice;
        int i = payType;
        iArr[i] = iArr[i] + smsSinglePrice;
        smsCount++;
        updatePay(0);
        if (pointPaidPrice[0] >= pointTotalPrice[0]) {
            return Game_Draw.SFX_last;
        }*/
        return 1111;
    }

    public void OrderSingleGame(int type) {
        /*if (type == -1) {
            try {
                if (getSMSCode() != 9999) {
                    confirmStr[0] = "连接网络失败，是否重试？";
                    setPayMode(4);
                }
                payAvailable = true;
            } catch (Exception e) {
                setPayMode(4);
            }
        } else {
            payType = type;
            int iStatus = activeGame();
            if (iStatus == 9999) {
                if (this.mode == 0 && type == 0) {
                    byte[] bArr = bPayStatus;
                    bArr[type] = (byte) (bArr[type] + 1);
                    bPayStatus[type] = 9;
                    if (bPayStatus[type] == 9) {
                        System.out.println("active!");
                        setPayMode(3);
                    } else {
                        System.out.println("keep paying! " + bPayStatus[type]);
                        p = null;
                        setPayMode(1);
                    }
                }
                updatePay(type);
            } else if (iStatus == 1111) {
                p = null;
                updateConfirmStr();
                setPayMode(1);
            } else {
                setPayMode(4);
            }
        }*/
    }

    public void run() {
        //OrderSingleGame(_type);
    }

    protected static void updatePay(int type) {
        /*OutputStream out = null;
        bPayStatus[4] = (byte) activateCnt;
        bPayStatus[5] = (byte) pointPaidPrice[0];
        try {
            out = Shell.sInstance.openFileOutput("buyGame.dat", 0);
            Log.v("SAVE: ", "buyGame.dat");
            out.write(bPayStatus);
            out.close();
        } catch (Exception e) {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e2) {
                }
            }
        }*/
    }

    public static byte getStatus(int id) {
        /*if (bPayStatus[1] == 0) {
            loadPay();
        }
        return bPayStatus[id];*/
        return 0;
    }

    public static void setStatus(int id, byte val) {
        /*bPayStatus[id] = val;
        updatePay(0);*/
    }

    private static void initialStatus() {
        /*bPayStatus[0] = 0;
        bPayStatus[1] = 0;
        bPayStatus[2] = 0;
        bPayStatus[3] = 0;*/
    }
    private static void loadPay() {
            /*int size;
        InputStream in = null;
        ByteArrayOutputStream out = null;
        try {
            in = Shell.sInstance.openFileInput("buyGame.dat");
            ByteArrayOutputStream out2 = new ByteArrayOutputStream();
            try {
                Log.v("OPEN: ", "buyGame.dat");
                do {
                    size = in.read(bPayStatus);
                } while (size > 0);
                out2.toByteArray();
                activateCnt = bPayStatus[4];
                pointPaidPrice[0] = bPayStatus[5];
                out2.close();
                in.close();
            } catch (Exception e) {
                out = out2;
                if (in != null) {
                    try {
                        in.close();
                    } catch (Exception e2) {
                    }
                }
                if (out != null) {
                    try {
                        out.close();
                    } catch (Exception e3) {
                    }
                }
            }
        } catch (Exception e4) {
        }*/
    }

    public static boolean isPayed(int type) {
        return true;
    }

    private static void setPayMode(int mode2) {
        /*strBack = null;
        startLine = 0;
        up = false;
        payMode = mode2;
        strBack = null;
        m_bScrollEnd = false;*/
    }

    public static int execPay(int key, int mode2, int type) {
        /*switch (payMode) {
            case 0:
                if (mode2 == 0) {
                    paidPrice = pointPaidPrice[0];
                    System.out.println("pay status #: " + bPayStatus[0] + "/" + type + "/");
                    if (isPayed(type)) {
                        return 2;
                    }
                }
                if ((key & 1) != 0) {
                    confirmStr[0] = "正在连接网络，请稍候……";
                    p = new Paying(0, -1);
                    p = null;
                    setPayMode(1);
                }
                if ((key & 2) != 0) {
                    exitSet();
                    return 1;
                }
                break;
            case 1:
                if (m_bScrollEnd) {
                    if ((key & 1) != 0 && payAvailable) {
                        setPayMode(2);
                    }
                    if ((key & 2) != 0) {
                        exitSet();
                        return 1;
                    }
                }
                break;
            case 2:
                if (p == null) {
                    p = new Paying(mode2, type);
                    break;
                }
                break;
            case 3:
                p = null;
                setPayMode(5);
                return 3;
            case 4:
                p = null;
                if ((key & 1) != 0) {
                    exitSet();
                    break;
                }
                break;
            case 5:
                if (m_bScrollEnd && ((key & 1) != 0 || mode2 == 1)) {
                    exitSet();
                    return 2;
                }
        }
        if ((key & 4) != 0) {
            scrollUp();
        } else if ((key & 8) != 0) {
            scrollDown();
        }*/
        return 0;
    }

    private static void exitSet() {
        /*p = null;
        payMode = 0;
        hRet = Define.PACKAGE_ID;
        status = Define.PACKAGE_ID;
        strBack = null;*/
    }

    public static void paintPay(Graphics g, int sx, int sy, int scx, int scy, int mode2, int type) {
        /*g.wg_setFontSize(24);
        g.setColor(0);
        g.fillRect(sx, sy, scx, scy);
        int x = sx;
        int cx = scx;
        int y = sy + 10;
        int cy = scy - 35;
        switch (payMode) {
            case 0:
            case 1:
                printWrap(g, confirmStr[type], x + (scx / 2), y, cx, cy, 1536);
                if (m_bScrollEnd != 0) {
                    g.setColor(Game_Draw.Color_White);
                    g.drawString("确认", 1.0f, (float) (scy - 1), 16640);
                    g.drawString("否", (float) (scx - 1), (float) (scy - 1), 20480);
                    return;
                }
                return;
            case 2:
                printWrap(g, Define.STR_WAIT, x + (scx / 2), y, cx, cy, 1536);
                return;
            case 3:
            case 5:
                printWrap(g, Define.STR_SUCCESS, x + (scx / 2), y, cx, cy, 1536);
                g.setColor(Game_Draw.Color_White);
                g.drawString("确定", 1.0f, (float) (scy - 1), 16640);
                return;
            case 4:
                printWrap(g, failStr, x + (scx / 2), y, cx, cy, 1536);
                g.setColor(Game_Draw.Color_White);
                g.drawString("重试", 1.0f, (float) (scy - 1), 16640);
                g.drawString("返回", (float) (scx - 1), (float) (scy - 1), 20480);
                return;
            default:
                return;
        }*/
    }

    private static void paintSK(Graphics g, String l, String r, int width, int height) {
        /*if (l != null) {
            g.drawString(l, 0.0f, (float) (height - 1), 16640);
        }
        if (r != null) {
            g.drawString(r, (float) (width - 1), (float) (height - 1), 20480);
        }*/
    }

    public static void scrollUp() {
        /*if (up) {
            startLine--;
        }*/
    }

    public static void scrollDown() {
        /*if (down) {
            startLine++;
        }*/
    }

    public static void printWrap(Graphics g, String str, int x, int y, int cx, int cy, int anchor) {
        /*int arrow_x;
        g.setColor(Game_Draw.Color_White);
        if (strBack == null) {
            strBack = getStrings(g, str, cx);
        }
        totalLines = strBack.length;
        if (startLine <= 0) {
            startLine = 0;
            up = false;
        } else {
            up = true;
        }
        if (totalLines - startLine <= cy / FONT_H) {
            down = false;
        } else {
            down = true;
        }
        if ((anchor & Graphics.HCENTER) != 0) {
            arrow_x = x;
        } else {
            arrow_x = (cx / 2) + x;
        }
        if (down != 0) {
            g.drawLine(arrow_x - 4, y + cy + 2, arrow_x + 4, y + cy + 2);
            g.drawLine(arrow_x - 2, y + cy + 3, arrow_x + 2, y + cy + 3);
            g.drawLine(arrow_x, y + cy + 4, arrow_x, y + cy + 4);
        } else {
            m_bScrollEnd = true;
        }
        if (up) {
            g.drawLine(arrow_x - 4, y - 2, arrow_x + 4, y - 2);
            g.drawLine(arrow_x - 2, y - 3, arrow_x + 2, y - 3);
            g.drawLine(arrow_x, y - 4, arrow_x, y - 4);
        }
        int dell = 0;
        int k = startLine;
        while (k < totalLines && (FONT_H * (dell + 1)) + y <= y + cy) {
            g.drawString(strBack[k], (float) x, (float) ((FONT_H * dell) + y), anchor);
            k++;
            dell++;
        }*/
    }

    public static String[] getStrings(Graphics g, String textString, int lineLength) {
        /*Vector strings = new Vector();
        int curWordIndex = 0;
        int textEndIndex = textString.length();
        String stringToDraw = null;
        do {
            int newWordIndex = curWordIndex;
            int newLineIndex = textString.indexOf(Text.STORY_52, newWordIndex);
            while (true) {
                int prevWordIndex = newWordIndex;
                String prevString = stringToDraw;
                newWordIndex = getWordIndex(textString, newWordIndex);
                if (newLineIndex > -1 && newLineIndex < newWordIndex) {
                    newWordIndex = newLineIndex;
                }
                stringToDraw = textString.substring(curWordIndex, newWordIndex).trim();
                if (g.stringWidth(stringToDraw) <= lineLength) {
                    if (newWordIndex != newLineIndex) {
                        if (newWordIndex >= textEndIndex) {
                            break;
                        }
                    } else {
                        newWordIndex++;
                        break;
                    }
                } else if (prevWordIndex == curWordIndex) {
                    int i = stringToDraw.length() - 1;
                    while (true) {
                        if (i <= 0) {
                            break;
                        }
                        String newString = stringToDraw.substring(0, i);
                        if (g.stringWidth(newString) <= lineLength) {
                            newWordIndex = prevWordIndex + i;
                            stringToDraw = newString;
                            break;
                        }
                        i--;
                    }
                } else {
                    newWordIndex = prevWordIndex;
                    stringToDraw = prevString;
                }
            }
            strings.addElement(stringToDraw);
            curWordIndex = newWordIndex;
        } while (curWordIndex < textEndIndex);
        String[] stringsToDraw = new String[strings.size()];
        strings.copyInto(stringsToDraw);
        return stringsToDraw;*/
        return new String[0];
    }

    private static int getWordIndex(String srcString, int fromIndex) {
        /*int spaceIndex;
        int toIndex;
        if (charIsWord(srcString.charAt(fromIndex))) {
            return fromIndex + 1;
        }
        while (true) {
            spaceIndex = srcString.indexOf(32, fromIndex);
            if (spaceIndex != 0) {
                break;
            }
            fromIndex++;
        }
        int toIndex2 = spaceIndex;
        if (toIndex2 == -1) {
            toIndex = srcString.length();
        } else {
            toIndex = toIndex2 + 1;
        }
        for (int i = fromIndex + 1; i < toIndex; i++) {
            if (charIsWord(srcString.charAt(i))) {
                return i;
            }
        }
        return toIndex;*/
        return 0;
    }

    private static boolean charIsWord(int theChar) {
        //return (theChar >= 11904 && theChar < 44032) || (theChar >= 63744 && theChar < 64256) || (theChar >= 65280 && theChar < 65504);
        return false;
    }
}
